﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//Thu Vien Them Vao
using QuanLyQuanCafe.BusinessLogic;
using QuanLyQuanCafe.BusinessObject;

namespace QuanLyQuanCafe
{
    public partial class FrmNhanVien : Form
    {
        private NhanVienBL nhanvienBL = new NhanVienBL();
        public FrmNhanVien()
        {
            InitializeComponent();
            cboGioiTinh.Items.Add("Nam");
            cboGioiTinh.Items.Add("Nữ");            
        }

        private void FrmNhanVien_Load(object sender, EventArgs e)
        {            

            dataGridViewNhanVien.DataSource = nhanvienBL.LayDanhSachNhanVien();
            AnHienButton(false);
            btnXoa.Enabled = false;
        }

        private void CapNhatText()
        {
            txtHoTen.Text = "";
            txtDiaChi.Text = "";
            datetimePickerNgaySinh.Value = DateTime.Now;
            cboGioiTinh.Text = "Nam";
            txtCongViec.Text = "";
            datetimePickerNgayVaoLam.Value = DateTime.Now;
            txtLuongCanBan.Text = "0";
            txtDienThoai.Text = "";
        }

        private void AnHienButton(bool flag)
        {
            btnThem.Enabled = !flag;
            btnSua.Enabled = !flag;
            //btnXoa.Enabled = !flag;
            btnLuu.Enabled = flag;
            btnHuy.Enabled = flag;

            txtCMND.Enabled = flag;
            txtHoTen.Enabled = flag;
            datetimePickerNgaySinh.Enabled = flag;
            txtDiaChi.Enabled = flag;
            cboGioiTinh.Enabled = flag;
            txtCongViec.Enabled = flag;
            datetimePickerNgayVaoLam.Enabled = flag;
            txtLuongCanBan.Enabled = flag;
            txtDienThoai.Enabled = flag;

            dataGridViewNhanVien.Enabled = !flag;
        }

        private void dataGridViewNhanVien_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            int row = e.RowIndex;
            try
            {
                txtCMND.Text = dataGridViewNhanVien.Rows[row].Cells["CMND_NV"].Value.ToString();
                txtHoTen.Text = dataGridViewNhanVien.Rows[row].Cells["HoTenNV"].Value.ToString();
                datetimePickerNgaySinh.Text = dataGridViewNhanVien.Rows[row].Cells["NgaySinh"].Value.ToString();
                txtDiaChi.Text = dataGridViewNhanVien.Rows[row].Cells["DiaChi"].Value.ToString();
                cboGioiTinh.Text = dataGridViewNhanVien.Rows[row].Cells["GioiTinh"].Value.ToString();
                txtCongViec.Text = dataGridViewNhanVien.Rows[row].Cells["CongViec"].Value.ToString();
                datetimePickerNgayVaoLam.Text = dataGridViewNhanVien.Rows[row].Cells["NgayVaoLam"].Value.ToString();
                txtLuongCanBan.Text = dataGridViewNhanVien.Rows[row].Cells["LuongCanBan"].Value.ToString();
                txtDienThoai.Text = dataGridViewNhanVien.Rows[row].Cells["SoDT"].Value.ToString();

            }
            catch
            {
            }
        }

        private NhanVienBO LayThongTinNhanVien()
        {
            NhanVienBO nhanvienBO = new NhanVienBO();
            nhanvienBO.CMND_NV = txtCMND.Text;
            nhanvienBO.HoTenNV = txtHoTen.Text;
            nhanvienBO.NgaySinh = Convert.ToDateTime(datetimePickerNgaySinh.Value);
            nhanvienBO.DiaChi = txtDiaChi.Text;
            nhanvienBO.GioiTinh = cboGioiTinh.Text;
            nhanvienBO.CongViec = txtCongViec.Text;
            nhanvienBO.NgayVaoLam = Convert.ToDateTime(datetimePickerNgayVaoLam.Value);
            nhanvienBO.LuongCanBan = float.Parse(txtLuongCanBan.Text);
            nhanvienBO.SoDT = txtDienThoai.Text;

            return nhanvienBO;
        }
        private void btnThem_Click(object sender, EventArgs e)
        {
            AnHienButton(true);
            CapNhatText();            
            txtCMND.Text = "";
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            AnHienButton(true);
            txtCMND.Enabled = false;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            try
            {
                NhanVienBO nhanvienBO = LayThongTinNhanVien();
                if (nhanvienBL.KiemTraCMNDTonTai(nhanvienBO.CMND_NV))
                {
                    if (nhanvienBL.SuaNhanVien(nhanvienBO))
                        FrmNhanVien_Load(sender, e);
                }
                else
                {
                    if (nhanvienBL.ThemNhanVien(nhanvienBO))
                        FrmNhanVien_Load(sender, e);
                }
            }
            catch
            {
                MessageBox.Show("Kiểm Lại Các Thông Tin Đã Nhập.", "Quán Lý Quán Cafe", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            AnHienButton(false);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn Có Muốn Xóa Nhân Viên [" + txtHoTen.Text + "] Không?", "Quản Lý Quán Cafe", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                if (nhanvienBL.XoaNhanVien(txtCMND.Text))
                    FrmNhanVien_Load(sender, e);
        }

      
    }
}
